home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr27 / gs26.zip / PS2EPSI.PS < prev    next >
Text File  |  1993-03-28  |  6KB  |  194 lines

  1. %    Copyright (C) 1990, 1991, 1993 Aladdin Enterprises.  All rights reserved.
  2. %    Distributed by Free Software Foundation, Inc.
  3. %
  4. % This file is part of Ghostscript.
  5. %
  6. % Ghostscript is distributed in the hope that it will be useful, but
  7. % WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. % to anyone for the consequences of using it or for whether it serves any
  9. % particular purpose or works at all, unless he says so in writing.  Refer
  10. % to the Ghostscript General Public License for full details.
  11. %
  12. % Everyone is granted permission to copy, modify and redistribute
  13. % Ghostscript, but only under the conditions described in the Ghostscript
  14. % General Public License.  A copy of this license is supposed to have been
  15. % given to you along with Ghostscript so you can know your rights and
  16. % responsibilities.  It should be in a file named COPYING.  Among other
  17. % things, the copyright notice and this notice must be preserved on all
  18. % copies.
  19.  
  20. % Convert an arbitrary PostScript file to an EPSI file.
  21. %
  22. %    NOTE: this file was contributed by a user: please contact
  23. %    George Cameron <george@bio-medical-physics.aberdeen.ac.uk>
  24. %    if you have questions.
  25. %
  26.  
  27. % Initialize, and redefine copypage and showpage.
  28. /ps2edict 25 dict def
  29. ps2edict begin
  30.  
  31.                 % The main procedure
  32.   /ps2epsi
  33.    {                % Open the file
  34.      outfile (w) file /epsifile exch def
  35.                     % Get the device parameters
  36.      currentdevice getdeviceprops dicttomark
  37.      dup /HWSize get aload pop
  38.        /devheight exch def
  39.        /devwidth exch def
  40.      /InitialMatrix get
  41.        /devmatrix exch def
  42.                 % Make a corresponding memory device
  43.      devmatrix devwidth devheight <ff 00>
  44.      makeimagedevice
  45.      /arraydevice exch def
  46.      arraydevice setdevice    % (does an erasepage)
  47.      /rowwidth devwidth 7 add 8 idiv def
  48.      /row rowwidth string def
  49.                 % Replace the definition of showpage
  50.      userdict /showpage { ps2edict begin epsipage end } bind put
  51.    } def
  52.  
  53.  /margintest
  54.  {
  55.       { dup arraydevice exch row copyscanlines pop
  56.     -1 0 1 rowwidth 1 sub
  57.         { dup row exch get 0 ne { exit }{ pop } ifelse
  58.         } for
  59.     dup -1 ne { exch pop } if
  60.     -1 ne { exit } if pop
  61.       } for
  62.  } bind def
  63.  
  64.   /epsipage
  65.    {
  66.      /loopcount devheight 1 sub def
  67.  
  68.      % Find top margin
  69.      -1 0 1 loopcount margintest
  70.      dup -1 eq { (blank page!!\n) print 0 .quit }{ exch pop } ifelse 
  71.      /tm exch def
  72.  
  73.      % Find bottom margin
  74.      loopcount -1 0 margintest
  75.      /bm exch def
  76.      
  77.      % Initialise limit variables
  78.      /loopcount rowwidth 1 sub def
  79.      /lm loopcount def /lmb 0 def
  80.      /rm 0 def /rmb 0 def
  81.  
  82.      % Find left and right boundaries of image
  83.      tm 1 bm
  84.       { % Get more data
  85.         arraydevice exch row copyscanlines pop
  86.         % Scan from left to find first non-zero element
  87.         % We save first the element, then the index
  88.     -1 0 1 loopcount
  89.         { dup row exch get dup 0 ne { exch exit }{ pop pop } ifelse
  90.         } for
  91.         % If we found -1, row is blank ..
  92.     dup -1 ne 
  93.         { % Find the leftmost index
  94.           dup lm lt
  95.           % If the new index is less, we save index and element
  96.           { /lm exch def /lmb exch def }
  97.           % If the index is equal, we or the bits together
  98.           { lm eq { lmb or /lmb exch def }{ pop } ifelse
  99.           } ifelse
  100.       % Now find the rightmost index
  101.       loopcount -1 0
  102.           { dup row exch get dup 0 ne { exch exit }{ pop pop } ifelse
  103.           } for
  104.       dup rm gt
  105.           % If the new index is greater, we save index and element
  106.           { /rm exch def /rmb exch def }
  107.           % If the index is equal, or the bits
  108.           { rm eq { rmb or /rmb exch def } { pop } ifelse
  109.           } ifelse
  110.         } if
  111.     pop
  112.       } for
  113.  
  114.      % Now we find the real left & right bit positions
  115.      256 0 1 7
  116.      { exch 2 div dup lmb le { pop exit }{ exch pop } ifelse
  117.      } for
  118.      /lmb exch def
  119.  
  120.      1 7 -1 0
  121.      { exch dup dup rmb and eq { pop exit }{ 2 mul exch pop } ifelse
  122.      } for
  123.      /rmb exch def
  124.  
  125.      % Calculate the bounding box values
  126.      /llx lm 8 mul lmb add def
  127.      /lly devheight bm sub def
  128.      /urx rm 8 mul rmb add def
  129.      /ury devheight tm sub def
  130.  
  131.      % Write out the magic string and bounding box information
  132.      epsifile (%!PS-Adobe-2.0 EPSF-1.2\n) writestring
  133.      epsifile (%%BoundingBox: ) writestring
  134.      epsifile llx write==only epsifile ( ) writestring
  135.      epsifile lly write==only epsifile ( ) writestring
  136.      epsifile urx write==only epsifile ( ) writestring
  137.      epsifile ury write==
  138.      epsifile (%%BeginPreview: ) writestring
  139.      epsifile urx llx sub 1 add write==only epsifile ( ) writestring
  140.      epsifile bm tm sub 1 add write==only epsifile ( 1 ) writestring
  141.      epsifile bm tm sub 1 add write==
  142.  
  143.      % Define character and bit widths for the output line buffer
  144.      /cwidth rm lm sub 1 add 8 mul 7 add 8 idiv def
  145.      /bwidth cwidth 8 mul def
  146.      /owidth urx llx sub 1 add 7 add 8 idiv def
  147.      /out cwidth string def
  148.  
  149.      % Create a 1-bit-high device for bitblt to align with the bbox
  150.      matrix cwidth 8 mul 1 <00 ff> makeimagedevice 
  151.      /linedevice exch def
  152.  
  153.      % 'image' a zero string to clear the line device
  154.      bwidth 1 1 matrix cwidth string linedevice setdevice image
  155.      arraydevice setdevice
  156.  
  157.      tm 1 bm
  158.       { % Get a scan line interval from the array device
  159.     arraydevice exch row copyscanlines lm cwidth getinterval
  160.     lmb 0 gt
  161.     { % 'image' it into the line device with the lmb offset
  162.       bwidth 1 1 [1 0 0 1 lmb 0] 5 -1 roll linedevice setdevice image
  163.       % Now we get the modified scan line
  164.       linedevice 0 out copyscanlines 0 owidth getinterval
  165.       % Reset the current device
  166.       arraydevice setdevice
  167.     } if
  168.     % Write out the hex data
  169.         epsifile (% ) writestring 
  170.         epsifile exch writehexstring
  171.     epsifile (\n) writestring
  172.       } for
  173.  
  174.      epsifile (%%EndImage\n) writestring
  175.      epsifile (%%EndPreview\n) writestring
  176.      epsifile flushfile
  177.      erasepage initgraphics
  178.  
  179.      DonePage 0 1 put
  180.    } bind def
  181.  
  182.  
  183. (outfile) getenv
  184.   { /outfile exch def 
  185.     ps2epsi
  186.  
  187.     /DonePage 1 string def
  188.     (%stdin) (r) file cvx execute0
  189.     DonePage 0 get 0 eq { showpage } if
  190.   } if
  191.  
  192. end
  193. quit
  194.